<template>
	<view class="return">
		<view class="bar_top">
			<view class="bar_list">
				<view :class="barNum == 0 ? 'bar_item bar_active' : 'bar_item'" @tap="bar(0)">
					<text>我发起的</text>
				</view>
				<view :class="barNum == 1 ? 'bar_item bar_active' : 'bar_item'" @tap="bar(1)">
					<text>待审批</text>
				</view>
				<view :class="barNum == 2 ? 'bar_item bar_active' : 'bar_item'" @tap="bar(2)">
					<text>待审核</text>
				</view>
				<view :class="barNum == 3 ? 'bar_item bar_active' : 'bar_item'" @tap="bar(3)">
					<text>已审核</text>
				</view>
			</view>
		</view>
		
		<block v-if="!boxHide">
			<view class="return_list">
				<view class="order_title">共{{list.length}}条</view>
				<block v-for="(item,index) in list">

					<view class="order_item" :key="index">
						<view class="order_item_title" @tap="navGo(item)">
							<view class="order_item_title_text">
								<view>{{item.customer_address}}</view>
								<view>{{item.customer_name}}/{{item.customer_title}}</view>
							</view>
							<!-- <image src="/static/images/table.png" @tap="go('info')"></image> -->
						</view>
						
						<block v-for="(item,index) in item.product_list">
							<view class="order_item_num" :key="index">
								<view>{{item.product_name}}</view>
								<view>数量{{item.number}}</view>
							</view>
						</block>
						
						<view class="order_item_content">
							<view class="font_item">
								<view>收货地址</view><view>{{item.customer_address}}</view>
							</view>
							<view class="font_item">
								<view>订单编号</view><view>{{item.order_no}}</view>
							</view>
							<view class="font_item">
								<view>开单日期</view><view>{{item.create_at}}</view>
							</view>
							<view class="font_item">
								<view>成交日期</view><view>{{item.close_ats?item.close_ats:''}}</view>
							</view>
							<view class="font_item">
								<view>收款期限</view><view>{{item.sk_date?item.sk_date:''}}</view>
							</view>
							<view class="font_item">
								<view>交货期限</view><view>{{item.jh_date}}</view>
							</view>
							<view class="font_item">
								<view>订单进度</view><view>{{item.schedule == null ? "-" : item.schedule}}</view>
							</view>
							<view class="font_item">
								<view>订单备注</view><view>{{item.desc}}</view>
							</view>
							
							<view class="order_item_price">
								<text>¥</text><text>{{item.total_price}}</text><text>意向（{{item.is_pay == 0 ? '未收' : '已收'}}）</text>
							</view>
							
							<view class="order_item_user" v-if="item.r_approval_status == 0 && item.r_approval_id != userId">
								<!-- <image></image> -->
								<view>{{item.r_user_name}}</view>
								<view>发起等待</view>
								<view>{{item.r_approval_name}}</view>
								<view>审批</view>
							</view>
							
							<view class="order_item_user" v-if="item.r_approval_status != 0 ">
								<!-- <image></image> -->
								<view>{{item.r_user_name}}</view>
								<view :class="item.r_approval_status == 2 ? 'red' : ''">{{item.r_approval_status == 1 ? '已通过审批' : '已拒绝审批'}}</view>
							</view>

							<view class="order_item_user" v-if="item.r_approval_status == 1 && item.r_status==0 && item.r_audit_id != userId">
								<!-- <image></image> -->
								<view>{{item.r_user_name}}</view>
								<view>发起等待</view>
								<view>{{item.r_audit_name}}</view>
								<view>审核</view>
							</view>

							<view class="order_item_user" v-if="item.r_status != 0 ">
								<!-- <image></image> -->
								<view>{{item.r_user_name}}</view>
								<view :class="item.r_approval_status == 2 ? 'red' : ''">{{item.r_approval_status == 1 ? '已通过审核' : '已拒绝审核'}}</view>
							</view>

							<view class="order_item_btn" v-else-if="item.r_approval_status == 0 && item.r_approval_id == userId && isApproval== 1"><text @tap="approver(item,1)">审批</text></view>

							<view class="order_item_btn" v-else-if="item.r_approval_status == 1 && item.r_status == 0 && item.r_audit_id == userId && isAudit== 1"><text @tap="approver(item,2)">审核</text></view>
	
						</view>
					</view>

				</block>
			</view>
			
			<view class="bottom_btn" v-if="checkPermission([100001])">
				<view class="bottom_btn_list">
					<view @tap="add()">新增审批</view>
				</view>
			</view>
		</block>
		
		<Loading ref="load"></Loading>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				barNum:0,
				list:[],
				loading:true,
				boxHide:true,
				userId:"",
				isAudit: -1, // 审核权限：1.审核  -1.没权限
				isApproval:-1 // 审批权限：1.审批 -1.没权限
			}
		},
		onShow() {
			this.userId = uni.getStorageSync("userId")
			this.isAudit = this.$app.globalData.isAudit;
			this.isApproval =  this.$app.globalData.isApproval;
			if(this.$app.globalData.upData.return){
				this.getData(1)
			}

		},
		onReachBottom() {
			if(this.loading){
				this.getData(3)
			}
		},
		onUnload() {
			this.$app.globalData.upData.return = true
		},
		methods:{
			bar(index){
				if(this.barNum != index){
					this.barNum = index;
					this.getData(1);
				}
			},
			navGo(item){//页面跳转
				uni.navigateTo({
					url:"/pages/return/info/info?id=" + item.r_id
				})
			},
			add(){
				uni.navigateTo({
					url:"/pages/return/add/add"
				})
			},
			approver(item,type){
				uni.navigateTo({
					url:"/pages/return/approve/approve?id=" + item.r_id + "&order_id=" + item.id + "&type="+type
				})
			}, 
			getData(type){
				this.$refs.load.initLoading({loadType:type}).then(res =>{
					this.boxHide = false;
					if(type == 1){this.list = [];this.page = 1;this.loading = true;this.$app.globalData.upData.return = false}
					let upJson = {page:this.page}
					if(this.barNum == 0) upJson.user_id = uni.getStorageSync("userId")
					if(this.barNum == 1) upJson.status = 0;
					if(this.barNum == 2) upJson.status = 1;
					if(this.barNum == 3) upJson.status = 2;
					this.$api.getReturnList(upJson).then(res => {
						this.$refs.load.getLoadingData({oldData:this.list,newData:res.data}).then(res => {
							this.list = res.list;
							this.loading = res.loading;
							this.page ++;
						});
					})
				})
			}
		}
	}
</script>

<style>
	.return{padding-bottom: 100upx;}
	.return_list{margin-top: 100upx;}
	.order_item{margin-top: 30upx;}
	.order_item:nth-child(2){margin-top: 0;}
	.order_item_content{padding-bottom: 0;}
	.order_item_user{padding-bottom: 30rpx;}
	.order_item_user view.red{color: #DD524D;}
</style>
